package com;

import com.Utils.FileUtil;
import com.Utils.Log;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.util.Date;
import java.util.Map;


public class MakeHtml {
    public static void main(String[] args) throws IOException, TemplateException {
        run("C:\\Users\\Administrator\\Desktop\\plant2",
                "C:/Users/Administrator/Desktop/so/htmlScript/plantMitochondria",
                "aa",
                "plant-Nanopore");

    }
    public static void run(String packagepath,String mobanpath,String htmlname,String moban) throws IOException, TemplateException {
        //String pppath = "E:\\projects\\GenHtml2.0\\src\\main\\resources\\"+moban+".properties";

        String pppath = "/lustre/sdb/xueyj/htmlScript/config/"+moban+".properties";
        Map<String, Object> stringStringMap = Copyneed.needMap(packagepath, mobanpath, pppath,moban,htmlname);
        if(stringStringMap == null){
            printlog();
            return;
        }
        System.out.println("--png html generated successfully--");
        makeHtml(mobanpath,moban,stringStringMap,htmlname,packagepath);
        System.out.println("--Page generated successfully:"+htmlname+".html"+"--");
        copyNeed(packagepath,mobanpath);
        System.out.println("--copy css js supplementary generated successfully--");
        Log.printlog(packagepath,true,htmlname,moban);
    }

    private static void printlog() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("/lustre/sdb/xueyj/htmlScript/config/log.txt"));
        String s = reader.readLine();
        while(s != null){
            System.out.println(s);
            s = reader.readLine();
        }
        reader.close();
    }

    public static void makeHtml(String mobanpath, String moban, Map<String, Object> map, String htmlname, String packagepath) throws IOException, TemplateException {

        // 第一步：创建一个 Configuration 对象，直接 new 一个对象。构造方法的参数就是 freemarker的版本号。
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 第二步：设置模板文件所在的路径。
        configuration.setDirectoryForTemplateLoading(new File(mobanpath));
        // 第三步：设置模板文件使用的字符集。一般就是 utf-8.
        configuration.setDefaultEncoding("UTF-8");
        // 第四步：加载一个模板，创建一个模板对象。
        Template template = null;
        template = configuration.getTemplate(moban + ".ftl");
        // 第五步：创建一个模板使用的数据集，可以是 pojo 也可以是 map。一般是 Map。
        map.put("foundDate",new Date());
        // 第六步：创建一个 Writer 对象，一般创建一 FileWriter 对象，指定生成的文件名。
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(packagepath + File.separator + htmlname + ".html")), "utf-8"));
        // 第七步：调用模板对象的 process 方法输出文件。
        template.process(map, writer);
        // 第八步：关闭流
        writer.flush();
        writer.close();
    }

    private static void copyNeed(String dirPath,String moban) {
        String sp = File.separator;
        String css = moban+sp+"css";
        String js = moban+sp+"js";

        String supplementary = moban + sp +"supplementary";
        FileUtil.copyDir(css,dirPath);
        FileUtil.copyDir(js,dirPath);
        FileUtil.copyDir(supplementary,dirPath);
    }
}
